#include "Menu.h"
#include "Controller.h"
#include "Data.h"
#include "StateMachine.h"

void menu_state(Data* data, Control control){
	if(is_in_the_area(control.mouse_button_coor, data->menu.play) && control.mouse_button == LEFT){
		data->machine.menu.cur = PLAY;
	}
	if(is_in_the_area(control.mouse_button_coor, data->menu.quit) && control.mouse_button == LEFT){
		data->machine.menu.cur = QUIT;
	}
	if(is_in_the_area(control.mouse_button_coor, data->menu.new_game) && control.mouse_button == LEFT){
		data->machine.menu.cur = NEW_GAME;
	}
}

void menu_bridge_action(Data* data){
}

void menu_knot_action(Data* data, Rough* rough){
	if(data->machine.menu.cur != data->machine.menu.pre){
		switch(data->machine.menu.cur){
			case PLAY:
				data->machine.main.cur = GAME;
				data->machine.menu.cur = SELECT;
				break;
			case QUIT:
				data->machine.main.cur = QUIT;
				data->machine.menu.cur = SELECT;
				break;
			case NEW_GAME:
				data->machine.menu.cur = SELECT;
				MonsterData(data, rough);
				TimeData(data, rough);
				GameData(data, rough);
				data->machine.game.cur = rough->d[50];
				data->machine.game.pre = rough->d[50];
				data->machine.creep.cur = rough->d[51];
				data->machine.creep.pre = rough->d[51];
				data->machine.pause.cur = rough->d[48];
				data->machine.pause.pre = rough->d[48];
				break;
		}
		data->machine.menu.pre = data->machine.menu.cur;
	}
}

void update_menu(Data* data, Rough* rough, Control control){
	if(data->machine.main.cur == MENU){
		menu_state(data, control);
		menu_bridge_action(data);
		menu_knot_action(data, rough);
	}
}

void display_menu(Data* data){
	if(data->machine.main.cur == MENU){
		SDL_Rect coorBlit;
		coorBlit.x = (data->graphic.window_dimension.x-data->menu.dimension.x)/2;
		coorBlit.y = (data->graphic.window_dimension.y-data->menu.dimension.y)/2;
		SDL_BlitSurface(data->menu.back_ground, NULL, data->graphic.screen, &coorBlit);
	}
}
